﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.SharePoint.Explorer;
using Microsoft.VisualStudio.SharePoint;
using SPVSX.Explorer.Extensions.Dialogs;
using System.Windows.Forms;
using SPVSX.Explorer.Extensions.CommandIds;

namespace SPVSX.Explorer.Extensions
{
    /// <summary>
    /// Represents an extension of SharePoint site nodes in Server Explorer 
    /// </summary>
    // Enables Visual Studio to discover and load this extension.
    [Export(typeof(IExplorerNodeTypeExtension))]
    // Indicates that this class extends SharePoint site nodes in Server Explorer.
    [ExplorerNodeType(ExplorerNodeTypes.SiteNode)]
    partial class SiteNodeExtension : IExplorerNodeTypeExtension
    {
        public void Initialize(IExplorerNodeType nodeType)
        {
            //Bind the events
            nodeType.NodeChildrenRequested += NodeChildrenRequested;
            nodeType.NodeMenuItemsRequested += NodeMenuItemsRequested;
            nodeType.NodePropertiesRequested += NodePropertiesRequested;
        }

        
        void NodeChildrenRequested(object sender, ExplorerNodeEventArgs e)
        {
            //throw new NotImplementedException();
        }

        void NodeMenuItemsRequested(object sender, ExplorerNodeMenuItemsRequestedEventArgs e)
        {
            e.MenuItems.Add("Developer Dashboard Settings").Click += SiteNodeExtension_Click;
        }

        void SiteNodeExtension_Click(object sender, MenuItemEventArgs e)
        {
             IExplorerNode owner = (IExplorerNode)e.Owner;

            string level =
            owner.Context.SharePointConnection.ExecuteCommand<string>(DeveloperDashboardCommandIds.GetDeveloperDashBoardDisplayLevelSetting);


            DeveloperDashboardSettingsDialog frm = new DeveloperDashboardSettingsDialog();
            frm.SelectedLevel = level;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                owner.Context.SharePointConnection.ExecuteCommand(DeveloperDashboardCommandIds.SetDeveloperDashBoardDisplayLevelSetting, frm.SelectedLevel);
            }
        }

        void NodePropertiesRequested(object sender, ExplorerNodePropertiesRequestedEventArgs e)
        {
            //throw new NotImplementedException();
        }

    }
}
